package com.ossjk.oa.accounting.controller;


import java.util.*;

import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.oa.accounting.service.ICwCpttypesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.oa.accounting.entity.CwReceivables;
import com.ossjk.oa.accounting.service.ICwReceivablesService;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chair
 * @since 2020-05-14
 */
@RestController
@RequestMapping("/accounting/cwReceivables")
public class CwReceivablesController extends BaseController {

	@Autowired
	private ICwCpttypesService iCwCpttypesService;
	@Autowired
	private ICwReceivablesService iCwReceivablesService;
	
	//@RequiresPermissions("0000")

	@RequestMapping("/list")
	public Object list(String mth, HttpServletRequest req) {

		if (mth == null) {
			Calendar now = Calendar.getInstance();
			if (now.get(Calendar.DAY_OF_MONTH) > 20) {
				mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
			} else {
				mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
			}
		}
		Wrapper wrapper = Condition.create().isWhere(true).like("needrecDate",mth);
		Map datas = new HashMap();
		datas.put("navi", iCwReceivablesService.selectPreOrNextByMth(mth));
		datas.put("datas", iCwReceivablesService.selectCapitalsByMth(wrapper));

		return responseSuccess(datas);

	}

	@PostMapping("/insert")
	public Object insert(CwReceivables cw_receivables) {
		Wrapper wrapper = Condition.create();

		if (iCwReceivablesService.insert(cw_receivables)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}


	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		return responseSuccess(iCwReceivablesService.selectById(id));
	}

 
	@PutMapping("/update")
	public Object update(CwReceivables cw_receivables,String cptname, String cpttype) {
		if(iCwReceivablesService.cwUpdateReceivables(cw_receivables,cptname,cpttype)){
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iCwReceivablesService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
	@GetMapping("/selectCpttype")
	public Object selectCpttype() {
		return responseSuccess(iCwCpttypesService.selectCwCpttypesInuse());
	}

	@PostMapping("/huanzhang/{id}")
	public ResponseBean huanzhang(@PathVariable("id") String id) {
		System.out.println(id);
		CwReceivables cwReceivables = new CwReceivables();
		cwReceivables.setId(id);
		cwReceivables.setFlag(3);

		boolean b = iCwReceivablesService.updateById(cwReceivables);
		if(b){
			return (ResponseBean) responseSuccess("操作成功");
		}else {
			return (ResponseBean) responseFail("操作失败");
		}

	}

}

